package com.mzj.saas.mservice.sys.service;

import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.StatusCode;
import com.mzj.saas.commons.vo.redis.TokenRedisVo;
import com.mzj.saas.mservice.announcement.service.AnnouncementService;
import com.mzj.saas.mservice.department.entity.Department;
import com.mzj.saas.mservice.department.repository.DepartmentRepository;
import com.mzj.saas.mservice.oss.service.OSSService;
import com.mzj.saas.mservice.sys.entity.AppUser;
import com.mzj.saas.mservice.sys.entity.Feedback;
import com.mzj.saas.mservice.sys.repository.AppUserRepository;
import com.mzj.saas.mservice.sys.repository.FeedbackRepository;
import com.mzj.saas.mservice.sys.vo.FeedbackVo;
import com.mzj.saas.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FeedbackService {
    private static final Logger LOG = LoggerFactory.getLogger(AnnouncementService.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private OSSService ossService;
    /**
     * OSS 域名
     */
    @Value("${ali.oss.endpoint}")
    private String endpoint;
    /*
     *oss地址
     */
    @Value("${ali.oss.bucket}")
    private String bucket;
    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    @Autowired
    private FeedbackRepository feedbackRepository;
    @Autowired
    private AppUserRepository appUserRepository;
    @Autowired
    private DepartmentRepository departmentRepository;


    public ResultBean<Object> save(String accessToken, FeedbackVo vo) {
        LOG.info("AnnouncementService save accessToken={},vo={},file={}", accessToken, vo.toString());
        ResultBean<Object> result = new ResultBean<>();
        result.setMsg(StatusCode.SUCCESS_CODE_10000.getErrorMsg());
        result.setCode(StatusCode.SUCCESS_CODE_10000.getErrorCode());
        TokenRedisVo tokenRedisVo = redisService.findTokenVo(accessToken);
        try {
            if (tokenRedisVo == null) {
                result.setCode(StatusCode.ERROR_CODE_40001.getErrorCode());
                result.setMsg(StatusCode.ERROR_CODE_40001.getErrorMsg());
                return result;
            }
            if (vo == null) {
                LOG.warn("AdvertisementService save vo is NULL");
                return ResultBean.failedResultOfParamWithMsg(StatusCode.ERROR_CODE_10004.getErrorMsg());
            }
            String userId = tokenRedisVo.getId();
            String name = "null";
            String collect = "null";
            //根据用户Id查询用户信息
            AppUser byIdAndStatus = appUserRepository.findByIdAndStatus(userId, 1);
            if (byIdAndStatus != null) {
                name = byIdAndStatus.getName();
            }
            //新增企业用户提交APP意见反馈显示提交人名字 xiaolong 20230413
            Integer loginType = tokenRedisVo.getLoginType();
            if (loginType == 1 ) {
                Department allByIdAndStatus = departmentRepository.findAllByIdAndStatus(tokenRedisVo.getEnterpriseId(), 1);
                name = allByIdAndStatus.getName();
            }
            // if (fileList != null) {
            //     for (MultipartFile file : fileList) {
            //
            //         ResultBean<Object> objectResultBean = ossService.putFile(bucket, file, OSSUploadFileTypeEnum.REPORT.toString());
            //         String picUrl = String.valueOf(objectResultBean.getResultData());
            //         homePic = ossService.getOssFilePath(picUrl);
            //         objects.add(homePic);
            //     }
            //     //图片地址
            //     String collect = objects.stream().collect(Collectors.joining(","));
            //     feedbackVo.setImg(collect);
            // }
            List<String> imgs = vo.getImg();
            if (imgs != null) {

                collect = String.join(",", imgs);
            }
            Feedback feedback = new Feedback();
            feedback.setImg(collect);
            feedback.setUserId(userId);
            feedback.setContent(vo.getContent());
            feedback.setUserName(name);
            feedback.setReleaseTime(new Date());
            feedback.setType(vo.getType());
            feedback.setStatus(0);
            feedbackRepository.save(feedback);

        } catch (Exception e) {
            LOG.error("AnnouncementService save Exception = {}", e);
            result.setCode(StatusCode.ERROR_CODE_10001.getErrorCode());
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
